package com.ts.proj.service.impl;

import com.ts.base.service.impl.CommonServiceImpl;
import com.ts.common.constant.InsUpdDelFlagConstant;
import com.ts.proj.entity.SystemConstantEntity;
import com.ts.proj.repository.SystemConstantRepository;
import com.ts.proj.service.SystemConstantService;
import com.ts.util.PageData;
import com.ts.util.PinyinHelperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SystemConstantServiceImpl extends CommonServiceImpl<SystemConstantEntity> implements SystemConstantService {

    @Autowired
    private SystemConstantRepository systemConstantRepository;


    @Override
    public List<SystemConstantEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<SystemConstantEntity> sp = this.getSpecification(pageData,SystemConstantEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return systemConstantRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<SystemConstantEntity> sp = this.getSpecification(pageData,SystemConstantEntity.class);
        return systemConstantRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<SystemConstantEntity> iterable) {
        systemConstantRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<SystemConstantEntity> saveAll(List<SystemConstantEntity> systemConstantEntities) {
        return systemConstantRepository.saveAll(systemConstantEntities);
    }

    @Override
    public boolean deleteAll() {
        return false;
    }

    @Override
    public List<SystemConstantEntity> findAllById(Integer[] ids) {
        List<SystemConstantEntity> list = systemConstantRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            SystemConstantEntity systemConstantEntity = new SystemConstantEntity();
            list.add(systemConstantEntity);
        }
        return list;
    }

    @Override
    public List<PageData> getConstantValueDesc(String constantValueKey) {
        PageData constantParamPd = new PageData();
        constantParamPd.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
        //获取项目分类
        constantParamPd.put("constantKey", constantValueKey);
        List<SystemConstantEntity> all = this.findAll(constantParamPd);
        List<PageData> list = new ArrayList<>();
        for(SystemConstantEntity entity : all){
            PageData pd = new PageData();
            pd.put("constantValuePY", PinyinHelperUtil.getPinYinHeadChar(entity.getConstantValueDesc()));
            pd.put("constantValue", entity.getConstantValue());
            pd.put("constantValueDesc", entity.getConstantValueDesc());
            list.add(pd);
        }
        return list;
    }
}
